const express = require('express');
const router = express.Router();

// 导入路由模块
const authRoutes = require('./authRoutes');
const userRoutes = require('./userRoutes');
const batchRoutes = require('./batchRoutes');

// API版本信息
router.get('/', (req, res) => {
  res.json({
    success: true,
    code: 200,
    message: '茶叶溯源系统API',
    data: {
      version: '1.0.0',
      description: '基于区块链的茶叶溯源系统后端API',
      endpoints: {
        auth: '/api/v1/auth',
        users: '/api/v1/users',
        batches: '/api/v1/batches'
      },
      documentation: '/api/v1/docs'
    },
    timestamp: new Date().toISOString()
  });
});

// 注册路由
router.use('/auth', authRoutes);
router.use('/users', userRoutes);
router.use('/batches', batchRoutes);

// API文档占位符
router.get('/docs', (req, res) => {
  res.json({
    success: true,
    message: 'API文档',
    data: {
      title: '茶叶溯源系统API文档',
      version: '1.0.0',
      baseUrl: `${req.protocol}://${req.get('host')}/api/v1`,
      endpoints: [
        {
          group: '认证相关',
          routes: [
            { method: 'POST', path: '/auth/register', desc: '用户注册' },
            { method: 'POST', path: '/auth/login', desc: '用户登录' },
            { method: 'POST', path: '/auth/refresh', desc: '刷新令牌' },
            { method: 'POST', path: '/auth/logout', desc: '用户登出' },
            { method: 'GET', path: '/auth/me', desc: '获取当前用户信息' },
            { method: 'PUT', path: '/auth/change-password', desc: '修改密码' },
            { method: 'POST', path: '/auth/forgot-password', desc: '忘记密码' },
            { method: 'POST', path: '/auth/reset-password', desc: '重置密码' }
          ]
        },
        {
          group: '用户管理',
          routes: [
            { method: 'GET', path: '/users', desc: '获取用户列表（管理员）' },
            { method: 'GET', path: '/users/stats', desc: '获取用户统计（管理员）' },
            { method: 'GET', path: '/users/:userId', desc: '获取用户详情' },
            { method: 'PUT', path: '/users/:userId', desc: '更新用户信息' },
            { method: 'PUT', path: '/users/:userId/status', desc: '更新用户状态（管理员）' },
            { method: 'PUT', path: '/users/:userId/role', desc: '更新用户角色（管理员）' },
            { method: 'PUT', path: '/users/:userId/reset-password', desc: '重置用户密码（管理员）' },
            { method: 'DELETE', path: '/users/:userId', desc: '删除用户（管理员）' }
          ]
        },
        {
          group: '茶叶批次',
          routes: [
            { method: 'POST', path: '/batches', desc: '创建茶叶批次（茶农）' },
            { method: 'GET', path: '/batches', desc: '获取茶叶批次列表' },
            { method: 'GET', path: '/batches/search', desc: '搜索茶叶批次' },
            { method: 'GET', path: '/batches/stats', desc: '获取批次统计信息' },
            { method: 'GET', path: '/batches/:batchId', desc: '获取茶叶批次详情' },
            { method: 'PUT', path: '/batches/:batchId', desc: '更新茶叶批次信息' },
            { method: 'POST', path: '/batches/:batchId/production', desc: '添加生产记录（茶农）' },
            { method: 'POST', path: '/batches/:batchId/logistics', desc: '添加物流记录（物流）' },
            { method: 'POST', path: '/batches/:batchId/sales', desc: '添加销售记录（商家）' },
            { method: 'GET', path: '/batches/public/:batchId', desc: '获取批次公开信息（消费者）' }
          ]
        }
      ]
    },
    timestamp: new Date().toISOString()
  });
});

module.exports = router;